/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.domain;

import javax.persistence.*;
import java.util.Set;
import java.util.TreeSet;

/**
 * The BrainTrain User.  Defines the roles associated with a given user as
 * well as modules available for access. 
 * 
 * @author John Ericksen
 *
 */

@Entity
@Table( name="braintrain_user" )
public class User extends BrainTrainBase implements Comparable<User> {

    private static final long serialVersionUID = 2615738936550593422L;

    private String userName;
    private String name;
    private Long profileId;

    private boolean disabled;

    
    private Set<Module> moduleAccess = new TreeSet<Module>();

    @ManyToMany(targetEntity=Module.class, fetch=FetchType.LAZY)
    @JoinTable(
            name="BRAINTRAIN_USER_MODULE_ACCESS",
            joinColumns={@JoinColumn(name="USER_ID")},
            inverseJoinColumns={@JoinColumn(name="MODULE_ID")}
        )
    public Set<Module> getModuleAccess() {
        return moduleAccess;
    }

    public void setModuleAccess(Set<Module> moduleAccess) {
        this.moduleAccess = moduleAccess;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
    @Column(unique=true)
    public Long getProfileId() {
		return profileId;
	}

	public void setProfileId(Long profileId) {
		this.profileId = profileId;
	}

	public int compareTo(User u) {
        if(this.getUserName() != null)
        {
            return this.getUserName().compareTo(u.getUserName());
        }
        return -1;
    }
    
    

	@Override
    public boolean equals(Object u) {
        if(this.getUserName() != null && u instanceof User)
        {
            return this.getUserName().equals(((User)u).getUserName());
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        if(this.getUserName() != null)
        {
            return this.getUserName().hashCode();
        }
        return -1;
    }

}
